package com.zhf.client;

import com.zhf.model.*;

import java.io.*;
import java.nio.channels.FileLock;
import java.util.ArrayList;
import java.util.List;

/**
 * 考题管理UI界面
 */
public class ClientExamUI {

    /**
     * 导出成绩
     * @param examUserListMessage
     * @param fileName
     */
    public static void exportExamUserList(ExamUserListMessage examUserListMessage, String fileName) {
        File file = new File(fileName);
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
            List<ExamUser> examUserList = examUserListMessage.getExamUserList();
            if(examUserList != null || !examUserList.isEmpty()){
                // 文件第一行导出学员的名字
                ExamUser examUser = examUserList.get(0);
                bufferedWriter.write("用户名：" + examUser.getUser().getUserName());
                bufferedWriter.newLine();
                for (ExamUser examUser1: examUserList) {
                    bufferedWriter.write(examUser1.getExamName());
                    bufferedWriter.newLine();
                    List<Question> questions = examUser1.getQuestions();
                    for (Question q: questions) {
                        bufferedWriter.write(q.getQuestionName());
                        bufferedWriter.newLine();
                        bufferedWriter.write(q.getOptionA());
                        bufferedWriter.newLine();
                        bufferedWriter.write(q.getOptionB());
                        bufferedWriter.newLine();
                        bufferedWriter.write(q.getOptionC());
                        bufferedWriter.newLine();
                        bufferedWriter.write(q.getOptionD());
                        bufferedWriter.newLine();
                        bufferedWriter.write(q.getAnswer());
                        bufferedWriter.newLine();
                    }
                    bufferedWriter.write(examUser1.getExamName() + " 对应的成绩为：" + examUser1.getGrade());
                    bufferedWriter.newLine();
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bufferedWriter != null){
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * 增加考题
     */
    public Question addExamQuestion(){
        System.out.print("请输入考题问题：");
        String questionName = ClientScanner.getScanner().next();
        System.out.print("请输入A选项：");
        String optionA = ClientScanner.getScanner().next();
        System.out.print("请输入B选项：");
        String optionB = ClientScanner.getScanner().next();
        System.out.print("请输入C选项：");
        String optionC = ClientScanner.getScanner().next();
        System.out.print("请输入D选项：");
        String optionD = ClientScanner.getScanner().next();
        System.out.print("请输入考题答案：");
        String answer = ClientScanner.getScanner().next();
        Question question = new Question(questionName, optionA, optionB, optionC, optionD, answer);
        return question;
    }

    public QuestionUpdVo updateQuestion(){
        System.out.println("请输入要修改的考题问题：");
        String originQuestionName = ClientScanner.getScanner().next();
        System.out.println("请输入修改后的考题问题：");
        String questionName = ClientScanner.getScanner().next();
        System.out.println("请输入修改后的A选项：");
        String optionA = ClientScanner.getScanner().next();
        System.out.println("请输入修改后的B选项：");
        String optionB = ClientScanner.getScanner().next();
        System.out.println("请输入修改后的C选项：");
        String optionC = ClientScanner.getScanner().next();
        System.out.println("请输入修改后的D选项：");
        String optionD = ClientScanner.getScanner().next();
        System.out.println("请输入修改后的考题答案：");
        String answer = ClientScanner.getScanner().next();
        Question question = new Question(questionName, optionA, optionB, optionC, optionD, answer);
        QuestionMessage questionMessage = new QuestionMessage(question, null);
        QuestionUpdVo questionUpdVo = new QuestionUpdVo(originQuestionName, questionMessage);
        return questionUpdVo;
    }

    public Question deleteExamQuestion(){
        System.out.println("请输入要删除的考题问题：");
        String questionName = ClientScanner.getScanner().next();
        return new Question(questionName, null,null,null,null,null);
    }

    public Question selectQuestion(){
        System.out.println("请输入要查找的考题问题");
        String questionName = ClientScanner.getScanner().next();
        return new Question(questionName, null,null,null,null,null);
    }

    public List<Question> importQuestion() throws IOException {
        System.out.println("请输入要导入考题对应的文件名：");
        String fileName = ClientScanner.getScanner().next();
        File file = new File(fileName);
        if(file.exists()){
            // 解析考题文件，一个考题总共有6行，第1行为考题问题，第 2 - 5 行 为考题选项， 第 6 行为考题答案
            // 使用文件流的方式读取文件内容到内存中，并解析考题选项
            FileInputStream fis = new FileInputStream(file);
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            String str = null;
            List<Question> questions = new ArrayList<>();
            Question question = new Question();
            int count = 0;
            while ((str = br.readLine()) != null) {
                // 连续读取6行内容
                count++;
                if(count == 1){
                    question.setQuestionName(str);
                }
                else if(count  == 2){
                    question.setOptionA(str);
                }
                else if(count == 3){
                    question.setOptionB(str);
                }
                else if(count == 4){
                    question.setOptionC(str);
                }
                else if(count == 5){
                    question.setOptionD(str);
                }
                else if(count == 6){
                    question.setAnswer(str);
                    Question questionTmp = new Question();
                    questionTmp.setQuestionName(question.getQuestionName());
                    questionTmp.setOptionA(question.getOptionA());
                    questionTmp.setOptionB(question.getOptionB());
                    questionTmp.setOptionC(question.getOptionC());
                    questionTmp.setOptionD(question.getOptionD());
                    questionTmp.setAnswer(question.getAnswer());
                    questions.add(questionTmp);
                    question = new Question();
                    count = 0;
                }
            }
            return questions;
        }
        else{
            return null;
        }
    }

    public ExamUser startExam(ExamUserMessage examUserMessage){
        ExamUser examUser = examUserMessage.getExamUser();
        List<Question> questions = examUser.getQuestions();
        for(int i = 0; i < questions.size(); i++){
            Question question = questions.get(i);
            System.out.println(question.getQuestionName());
            System.out.println(question.getOptionA());
            System.out.println(question.getOptionB());
            System.out.println(question.getOptionC());
            System.out.println(question.getOptionD());
        }
        for(int i = 0; i < questions.size(); i++){
            Question question = questions.get(i);
            System.out.print("请输入您的第 " + (i+1) + "题答案：");
            question.setAnswer(ClientScanner.getScanner().next());
        }
        return examUser;
    }

}
